package com.revenant.driver.ui.fragment;


import android.app.Fragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.revenant.driver.R;
import com.revenant.driver.adapter.VerticalTypeItemAdapter;
import com.revenant.driver.base.BaseFragment;
import com.revenant.driver.bean.AreaShowBean;
import com.revenant.driver.bean.ShipperBean.LoginUserBean;
import com.revenant.driver.bean.WebUrlBean;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.revenant.driver.utils.Constants.Look_Xieyi;
import static com.revenant.driver.utils.Constants.Publish_Goods;

/**
 * A simple {@link Fragment} subclass.
 */
public class PublishPlatformFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.timestart)
    TextView timestart;
    @BindView(R.id.timeend)
    TextView timeend;
    @BindView(R.id.publish_btn_goods)
    Button   publishBtnGoods;
    CityPickerView          mPicker = new CityPickerView();
    VerticalTypeItemAdapter adaptera;
    VerticalTypeItemAdapter adapterb;
    VerticalTypeItemAdapter adapterc;
    @BindView(R.id.nice_spinner)
    NiceSpinner  niceSpinner;
    @BindView(R.id.load_text)
    TextView     loadText;
    @BindView(R.id.load_detail_text)
    EditText     loadDetailText;
    @BindView(R.id.unload_text)
    TextView     unloadText;
    @BindView(R.id.unload_detail_text)
    EditText     unloadDetailText;
    @BindView(R.id.unload_personal_phone)
    EditText     unloadPersonalPhone;
    @BindView(R.id.unload_personal_name)
    EditText     unloadPersonalName;
    @BindView(R.id.goods_name)
    EditText     goodsName;
    @BindView(R.id.goods_type)
    TextView     goodsType;
    @BindView(R.id.goods_count)
    EditText     goodsCount;
    @BindView(R.id.goods_price)
    EditText     goodsPrice;
    @BindView(R.id.goods_weight)
    EditText     goodsWeight;
    @BindView(R.id.fee_show)
    LinearLayout feeShow;
    @BindView(R.id.show_bills)
    LinearLayout showBills;
    @BindView(R.id.offline_jiesuan)
    SwitchCompat offlineJiesuan;
    @BindView(R.id.auto_confirm_driver)
    SwitchCompat autoConfirmDriver;
    @BindView(R.id.make_out_bills)
    SwitchCompat makeOutBills;
    private List<String>       typelista     = Arrays.asList("4.2米以下",
            "4.2米", "5.6米", "6.2米", "6.8米", "7.2米", "8.2米", "8.6米", "9.6米", "11.7米", "12.5米", "13米", "13.5米", "15米", "16米", "17.5米", "18米");
    private List<String>       typelistb     = Arrays.asList("普通", "箱式", "罐式", "冷藏式", "保温式", "牵引式", "封闭", "平板", "集装", "自卸", "特殊结构", "仓栅式", "罐式", "集装箱", "厢式", "专项作业", "车辆运输", "平板", "自卸", "普通");
    private List<String>       typelistc     = Arrays.asList("煤炭", "石油", "金属", "钢铁", "矿建", "水泥", "木材", "非金属矿石", "化肥及农药", "盐", "粮食", "机械设备电器", "轻工原料", "有色金属", "轻工医药", "鲜活冷冻", "商品汽车", "其他");
    private List<AreaShowBean> showBeanLista = new ArrayList<>();
    private List<AreaShowBean> showBeanListb = new ArrayList<>();
    private List<AreaShowBean> showBeanListc = new ArrayList<>();
    private int                defaulta      = 0;
    private int                defaultb      = 0;
    private int                defaultc      = 0;
    private String             loadingCode;
    private String             unloadCode;
    private String             weburl;
    private boolean            isOnline      = false; /*线下结算*/
    private boolean            isAuto        = false; /*自动确认*/
    private boolean            isInvoice     = false;/*开具发票*/
    private String             goodsdanwei   = "1";

    @Override

    protected int setLayoutId() {
        return R.layout.fragment_publishplatform;
    }

    @Override
    protected void initView() {
        mPicker.init(getActivity());

        List<String> dataset = new LinkedList<>(Arrays.asList("吨", "方", "米", "件"));
        niceSpinner.attachDataSource(dataset);
//        offlineJiesuan=getActivity().findViewById(R.id.offline_jiesuan);
//        autoConfirmDriver=getActivity().findViewById(R.id.auto_confirm_driver);
//        makeOutBills=getActivity().findViewById(R.id.make_out_bills);

        getXieyi(2);


    }

    @Override
    public void initData() {

        AreaShowBean showBean = new AreaShowBean();
        showBean.setAreaname("不限车长");
        showBean.setSelect(1);
        showBeanLista.add(showBean);
        for (int i = 0; i < typelista.size(); i++) {
            AreaShowBean showBeana = new AreaShowBean();
            showBeana.setAreaname(typelista.get(i));
            showBeana.setSelect(0);
            showBeanLista.add(showBeana);
        }

        AreaShowBean showBeanb = new AreaShowBean();
        showBeanb.setAreaname("不限车型");
        showBeanb.setSelect(1);
        showBeanListb.add(showBeanb);
        for (int i = 0; i < typelistb.size(); i++) {
            AreaShowBean showBeanbb = new AreaShowBean();
            showBeanbb.setAreaname(typelistb.get(i));
            showBeanbb.setSelect(0);
            showBeanListb.add(showBeanbb);
        }

        AreaShowBean showBeanc = new AreaShowBean();
        showBeanc.setAreaname("不限");
        showBeanc.setSelect(1);
        showBeanListc.add(showBeanc);
        for (int i = 0; i < typelistc.size(); i++) {
            AreaShowBean showBeancc = new AreaShowBean();
            showBeancc.setAreaname(typelistc.get(i));
            showBeancc.setSelect(0);
            showBeanListc.add(showBeancc);
        }


        niceSpinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                goodsdanwei = String.valueOf(position + 1);

            }
        });

        offlineJiesuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    isOnline = true;
                    showBills.setVisibility(View.GONE);
                    feeShow.setVisibility(View.GONE);
                } else {
                    isOnline = false;
                    showBills.setVisibility(View.VISIBLE);
                    feeShow.setVisibility(View.VISIBLE);
                }

            }
        });


        autoConfirmDriver.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    isAuto = true;
                } else {
                    isAuto = false;

                }

            }
        });


        makeOutBills.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    isInvoice = true;
                } else {
                    isInvoice = false;
                }

            }
        });

//        offlineJiesuan.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
//            @Override
//            public void toggleToOn(SwitchView view) {
//                isOnline = true;
//                showBills.setVisibility(View.GONE);
//                feeShow.setVisibility(View.GONE);
//
//            }
//
//            @Override
//            public void toggleToOff(SwitchView view) {
//                isOnline = false;
//                showBills.setVisibility(View.VISIBLE);
//                feeShow.setVisibility(View.VISIBLE);
//            }
//        });
//
//
//        autoConfirmDriver.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
//            @Override
//            public void toggleToOn(SwitchView view) {
//                isAuto = true;
//
//            }
//
//            @Override
//            public void toggleToOff(SwitchView view) {
//                isAuto = false;
//
//            }
//        });
//
//        makeOutBills.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
//            @Override
//            public void toggleToOn(SwitchView view) {
//                isInvoice = true;
//
//            }
//
//            @Override
//            public void toggleToOff(SwitchView view) {
//                isInvoice = false;
//            }
//        });

    }


    public static PublishPlatformFragment newInstance(String title) {
        Bundle bundle = new Bundle();
        bundle.putString(title, title);
        PublishPlatformFragment fragment = new PublishPlatformFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


//    @OnClick({R.id.timestart, R.id.timeend, R.id.publish_btn_goods})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.timestart:
//                alertTypeSelect();
//                break;
//            case R.id.timeend:
//                break;
//            case R.id.publish_btn_goods:
//                break;
//            default:
//        }
//    }

    private void alerttime(TextView textView) {
        TimePickerView pvTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                textView.setText(getTime(date));
            }
        }).setType(new boolean[]{true, true, true, true, false, false})
                .build();
        pvTime.show();

    }

    private void alertarea(TextView textView) {
        CityConfig cityConfig = new CityConfig.Builder().confirTextColor("#1E90FF").cancelTextColor("#1E90FF").build();
        mPicker.setConfig(cityConfig);

        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                textView.setText(province + "-" + city + "-" + district);
                int viewId = textView.getId();
                switch (viewId) {
                    case R.id.load_text:
                        loadingCode = district.getId();
                        break;
                    case R.id.unload_text:
                        unloadCode = district.getId();
                        break;
                }
            }

            @Override
            public void onCancel() {
                ToastUtils.showLongToast(getActivity(), "已取消");
            }
        });

        mPicker.showCityPicker();
    }

    private void alertTypeSelect() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getLayoutInflater().inflate(R.layout.alert_mul_type_first, null);
        RecyclerView type1 = view.findViewById(R.id.recyclerview_type1);
        RecyclerView type2 = view.findViewById(R.id.recyclerview_type2);
        RecyclerView type3 = view.findViewById(R.id.recyclerview_type3);
        TextView clean = view.findViewById(R.id.clear_all_conditions);
        TextView confirm = view.findViewById(R.id.confirm);

        adaptera = new VerticalTypeItemAdapter();
        adapterb = new VerticalTypeItemAdapter();
        adapterc = new VerticalTypeItemAdapter();


        type1.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        type1.setAdapter(adaptera);
        adaptera.setNewData(showBeanLista);

        adaptera.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adaptera.getData().get(defaulta).setSelect(0);
                adaptera.getData().get(position).setSelect(1);
                adaptera.notifyItemChanged(defaulta);
                defaulta = position;
                adaptera.notifyItemChanged(position);

            }
        });


        type2.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        type2.setAdapter(adapterb);
        adapterb.setNewData(showBeanListb);
        adapterb.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adapterb.getData().get(defaultb).setSelect(0);
                adapterb.getData().get(position).setSelect(1);
                defaultb = position;
                adapterb.notifyDataSetChanged();
            }
        });


        type3.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        type3.setAdapter(adapterc);
        adapterc.setNewData(showBeanListc);
        adapterc.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adapterc.getData().get(defaultc).setSelect(0);
                adapterc.getData().get(position).setSelect(1);
                defaultc = position;
                adapterc.notifyDataSetChanged();
            }
        });

        builder.setView(view);


//        final Button deletebtn = view.findViewById(R.id.delete_btn);
        final AlertDialog alertDialog = builder.show();

        final Window window = alertDialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0));
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsType.setText(
                        adaptera.getData().get(defaulta).getAreaname() + "|" +
                                adapterb.getData().get(defaultb).getAreaname() + "|" +
                                adapterc.getData().get(defaultc).getAreaname()

                );
                alertDialog.dismiss();

            }
        });

        clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adaptera.getData().get(defaulta).setSelect(0);
                adaptera.getData().get(0).setSelect(1);
                adaptera.notifyDataSetChanged();

                adapterb.getData().get(defaultb).setSelect(0);
                adapterb.getData().get(0).setSelect(1);
                adapterb.notifyDataSetChanged();

                adapterc.getData().get(defaultc).setSelect(0);
                adapterc.getData().get(0).setSelect(1);
                adapterc.notifyDataSetChanged();

            }
        });
//        deletebtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                LogUtils.d("AAAAAAAA");
//                alertDialog.dismiss();
//            }
//        });
    }

    @OnClick({R.id.load_text, R.id.unload_text, R.id.goods_type, R.id.timestart, R.id.timeend, R.id.publish_btn_goods})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.load_text:
                alertarea(loadText);
                break;
            case R.id.unload_text:
                alertarea(unloadText);
                break;
            case R.id.goods_type:
                alertTypeSelect();
                break;
            case R.id.timestart:
                alerttime(timestart);
                break;
            case R.id.timeend:
                alerttime(timeend);
                break;
            case R.id.publish_btn_goods:
                if (emptyText(goodsName, "请输入货物名称")) {
                } else if (emptyText(loadText, "请选择装车地址")) {
                } else if (emptyText(loadDetailText, "请填写装车详细地址")) {
                } else if (emptyText(unloadText, "请填写卸车地址")) {
                } else if (emptyText(unloadDetailText, "请填写卸车详细地址")) {
                } else if (emptyText(goodsWeight, "请填写重量")) {
                } else if (emptyText(goodsCount, "请填写数量")) {
                } else if (emptyText(timestart, "请填写时间")) {
                } else if (emptyText(timeend, "请填写时间")) {
                } else if (emptyText(unloadPersonalName, "请填写项目")) {
                } else if (emptyText(unloadPersonalPhone, "请填写电话")) {
                } else if (emptyText(goodsPrice, "请填写价格")) {
                } else {
                    showDialog(weburl);

                }
                break;
        }
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        return format.format(date);
    }

    private String getCurrentTime() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(d);
        return str;
    }

    private void publish_goods(
            String name,
            String loading,
            String loadingCode,
            String unload,
            String unloadCode,
            String weight,
            String cube,
            String total,
            String accountId,
            String status,
            String isDel,
            String isPublish,
            String despatchDate,
            String recieveName,
            String recieveMobile,
            String isAuto,
            String isOnline,
            String price,
            String danWei,
            String vehicleLeader,
            String loadingDetail,
            String unloadingDetail,
            String loadDate,
            String isInvoice,
            String remark,
            String ids
    ) {
        OkGo.<String>post(Publish_Goods)

                .params("name", name)
                .params("loading", loading)
                .params("loadingCode", loadingCode)
                .params("unload", unload)
                .params("unloadCode", unloadCode)
                .params("weight", weight)
                .params("cube", cube)
                .params("total", total)
                .params("accountId", accountId)
                .params("status", status)
                .params("isDel", isDel)
                .params("isPublish", isPublish)
                .params("despatchDate", despatchDate)
                .params("recieveName", recieveName)
                .params("recieveMobile", recieveMobile)
                .params("isAuto", isAuto)
                .params("isOnline", isOnline)
                .params("price", price)
                .params("danWei", danWei)
                .params("vehicleLeader", vehicleLeader)
                .params("loadingDetail", loadingDetail)
                .params("unloadingDetail", unloadingDetail)
                .params("loadDate", loadDate)
                .params("isInvoice", isInvoice)
                .params("remark", remark)
//                .params("ids", ids)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        LoginUserBean s = GsonUtil.parseJsonWithGson(response.body(), LoginUserBean.class);
                        if (s.getCode().equals("0")) {
                            ToastUtils.showShortToast(getActivity(), "成功");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.showLongToast(getActivity(), response.body());
                    }
                });
    }

    private void showMessage(String msg) {
        ToastUtils.showShortToast(getActivity(), msg);
    }

    private boolean emptyText(TextView textView, String msg) {
        if (textView.getText().toString().isEmpty()) {
            showMessage(msg);
            return true;
        }
        return false;
    }

    private String getText(TextView textView) {
        return textView.getText().toString().trim();
    }

    private void showDialog(String url) {
        QMUIDialog.CustomDialogBuilder dialogBuilder = new QMUIDialog.CustomDialogBuilder(getActivity());
        dialogBuilder.setLayout(R.layout.dialog_xieyi);
        final QMUIDialog dialog = dialogBuilder.setTitle("协议")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        publish_goods(
                                getText(goodsName),
                                getText(loadText),
                                loadingCode,
                                getText(unloadText),
                                unloadCode,
                                getText(goodsWeight),
                                "",
                                getText(goodsCount),
                                String.valueOf(SPUtils.getAccounId(getActivity())),
                                "0",
                                "0",
                                "1",
                                getCurrentTime(),
                                getText(unloadPersonalName),
                                getText(unloadPersonalPhone),
                                String.valueOf(isAuto),
                                String.valueOf(isOnline),
                                getText(goodsPrice),
                                goodsdanwei,
                                getText(goodsType),
                                getText(loadDetailText),
                                getText(unloadDetailText),
                                String.valueOf(getText(timestart) + "-" + getText(timeend)),
                                String.valueOf(isInvoice),
                                "",
                                ""
                        );

                    }
                })
                .create();

        WebView displayImageView = (WebView) dialog.findViewById(R.id.webxieyi);
        displayImageView.loadUrl(url);
        dialog.show();
    }

    private void getXieyi(int status) {
        OkGo.<String>post(Look_Xieyi)
                .params("status", status)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        WebUrlBean webUrlBean = GsonUtil.parseJsonWithGson(response.body(), WebUrlBean.class);
                        if (webUrlBean.getCode().contains("0")) {
                            weburl = webUrlBean.getData().getUrl();
                        }
                    }
                });
    }
}
